﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Code.DAO
{
    public class WorktimeTableDAO : BaseDAO
    {
        private const string WORKTIME = "WORKTIME";
        private const string PROPERTY = "PROPERTY";

        public WorktimeTableDAO()
            : base ()
        {
        }

        public BLWorktimeTable Collect(BLJobTitle jobTitleFilter, Guid agentId, string fullName, int year, int month, int totalPage, int pageSize, int pageIndex)
        {
            int startIndex = (pageIndex - 1) * pageSize + 1;
            if (pageIndex > totalPage)
            {
                startIndex = (totalPage - 1) * pageSize + 1;
            }
            else if (pageIndex < 1)
            {
                startIndex = 1;
            }
            int endIndex = startIndex + pageSize - 1;

            BLWorktimeTable result = new BLWorktimeTable(year, month);
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@jobTitleId", jobTitleFilter.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@agentId", agentId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@fullName", fullName, ParameterDirection.Input));
            condition.Add(new BaseCondition("@filterDateDown", new DateTime(year, month, 1), ParameterDirection.Input));
            condition.Add(new BaseCondition("@filterDateUp", new DateTime(year, month, DateTime.DaysInMonth(year, month),23,59,59), ParameterDirection.Input));
            condition.Add(new BaseCondition("@startIndex", startIndex, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endIndex", endIndex, ParameterDirection.Input));
            DataTable data = select("usp_SearchWorktime", condition);
            List<Dictionary<string,object>> worktimes = new List<Dictionary<string, object>>();
            //Dictionary<int,BLWorktimeProperty> properties = new Dictionary<int,BLWorktimeProperty>();
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLWorktime rWorktime = new BLWorktime();
                rWorktime.Id = (Guid)data.Rows[i]["WorktimeId"];
                rWorktime.AgentId = (Guid)data.Rows[i]["AgentId"];
                string rFullName = (string)data.Rows[i]["FullName"];
                rWorktime.StartTime = (DateTime)data.Rows[i]["StartTime"];
                rWorktime.EndTime = (DateTime)data.Rows[i]["EndTime"];
                BLWorktimeProperty rProp = new BLWorktimeProperty(agentId, rWorktime.StartTime);
                rProp.Id = (Guid)data.Rows[i]["WorktimePropertyId"];
                rProp.AgentId = rWorktime.AgentId;
                rProp.FullName = rFullName;
                rProp.Description = (string)data.Rows[i]["Description"];
                rProp.Approved = (int)data.Rows[i]["IsApproved"];
                rProp.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                rProp.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];

                Dictionary<string, object> groupW = null;
                foreach (Dictionary<string,object> dic in worktimes)
                {
                    List<BLWorktime> lstW = (List<BLWorktime>)dic[WORKTIME];
                    BLWorktimeProperty prop = (BLWorktimeProperty)dic[PROPERTY];
                    foreach (BLWorktime w in lstW)
                    {
                        if (rWorktime.AgentId.Equals(w.AgentId) && (rWorktime.StartTime.Year == w.StartTime.Year)
                            && (rWorktime.StartTime.Month == w.StartTime.Month) && (rWorktime.StartTime.Day == w.StartTime.Day))
                        {
                            groupW = dic;
                            break;
                        }
                    }
                }
                if (groupW == null)
                {
                    groupW = new Dictionary<string, object>();
                    groupW.Add(WORKTIME, new List<BLWorktime>());
                    BLWorktimeProperty emptyProp = new BLWorktimeProperty(rWorktime.AgentId, rWorktime.StartTime);
                    emptyProp.FullName = rFullName;
                    groupW.Add(PROPERTY, emptyProp);
                    worktimes.Add(groupW);
                }
                if (!rWorktime.Id.Equals(Guid.Empty))
                {
                    ((List<BLWorktime>)groupW[WORKTIME]).Add(rWorktime);
                }
                if (!rProp.Id.Equals(Guid.Empty))
                {
                    groupW[PROPERTY] = rProp;
                }
            }

            foreach (Dictionary<string,object> dic in worktimes)
            {
                List<BLWorktime> w = (List<BLWorktime>)dic[WORKTIME];
                BLWorktimeProperty prop = (BLWorktimeProperty)dic[PROPERTY];
                result.SetCell(prop.AgentId, prop.FullName, prop.StartTime.Day, w, prop);
            }
            
            return result;
        }
    }
}
